<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Nouveau message</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link rel="stylesheet" type="text/css" href="default.css" />
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="scripts.js"></script>

    </head>
    <body>
        <div id="upbg"></div>
        <div id="outer">

            <?php
            include 'header.php';
            ?>
            <?php
            include 'login.php';
            include 'menu.php';
            ?>
            <div id="content">

                <?php
                include "connectBDD.php";

                           
                if (isset($_POST["messageEnvoye"]) == "ok") {
                    //On test si tout les champs ont bien été remplis.
                    if (!empty($_POST["destinataire"]) && !empty($_POST["message"])) {
                        $destinataire = $_POST["destinataire"];
                        $query = "select login from user where login = '$destinataire'";
                        $result = mysql_query($query) or die(mysql_error());
                        if (mysql_num_rows($result) <= 0) {
                            echo "<div style='color:red;'>Le membre '$destinataire' est introuvable </div>";
                        }else{
                            $mailing_date_time = date("Y-m-d H:i:s", time());
                            $query = "insert into message (login_sender, login_receiver, mailing_date_time, message_content)
                                        values ('".$_SESSION['login']."', '$destinataire', '$mailing_date_time','".addslashes($_POST['message'])."')";
                            $result = mysql_query($query) or die(mysql_error());
                            $query = "insert into message_sent (login_sender, login_receiver, mailing_date_time, message_content)
                                        values ('".$_SESSION['login']."', '$destinataire', '$mailing_date_time','".addslashes($_POST['message'])."')";
                            $result = mysql_query($query) or die(mysql_error());
                            
                            echo"<div style='color:green;'>Message envoyé</div>
                                 <script type='text/javascript'>fermerFenetre('Votre message a bien été envoyé');</script>";
                            
                            
                        }
                    }else
                        echo "<div style='color:red'>Veuillez renseigner tous les champs.</div>";
                }
                ?>

                <div id='blockInscription'>
                    <div class='sousTitreInfoInscription'>Nouveau message</div>

                    <form action="nouveauMessage.php" method="post"><p>
                            <label for="destinataire">Destinataire :</label><input type="text" name="destinataire" 
                                                                             value="<?php if(isset($_GET['login'])){echo $_GET['login'];} ?>" id="dest"/><br />
                            <label for="message">Message :</label><textarea rows="6" cols="40" name="message" id="message"/></textarea><br />
                            <label for="cache"></label><input type="hidden" name="messageEnvoye" value="ok"/><br/>
                            <input type="submit" value="envoyer le message" name="bouton"/>
                        </p></form>
                </div>                

            </div>
            <?php
            include 'footer.php';
            ?>

        </div>

    </body>
</html>

